<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      class="up_class"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="项目名称" prop="projectCode">
        <el-select
          @change="changeCode"
          style="width:100%"
          v-model="dataForm.projectCode"
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in optionsprojectId"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备序列号" prop="deviceSn">
        <!-- <el-input
          v-model="dataForm.deviceSn"
          placeholder="设备序列号"
        ></el-input> -->
        <el-select
          style="width:100%"
          v-model="dataForm.deviceSn"
          placeholder="请选择设备序列号"
        >
          <el-option
            v-for="item in optionsDeviceSn"
            :key="item.deviceSn"
            :label="item.name"
            :value="item.deviceSn"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人员名称" prop="personnelId">
        <el-select
          style="width:100%"
          v-model="dataForm.personnelId"
          placeholder="请选择人员"
        >
          <el-option
            v-for="item in optionspersonnelName"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="职位" prop="positionTitle">
      <el-input v-model="dataForm.positionTitle" placeholder="职位"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idNumber">
      <el-input v-model="dataForm.idNumber" placeholder="身份证号"></el-input>
    </el-form-item> -->
      <el-form-item label="考勤时间" prop="attendanceTime">
        <!-- <el-input v-model="dataForm.attendanceTime" placeholder="考勤时间"></el-input> -->
        <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.attendanceTime"
          type="datetime"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="进出口" prop="entryExit">
        <el-select
          style="width:100%"
          v-model="dataForm.entryExit"
          placeholder="请选择进出口"
        >
          <el-option
            v-for="item in optionsentryExit"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="打卡照" prop="clockingPhotos">
      <el-input v-model="dataForm.clockingPhotos" placeholder="打卡照"></el-input>
    </el-form-item> -->
      <el-form-item label="上传打卡照" prop="clockingPhotos">
        <!-- <el-input v-model="dataForm.imageBase64" placeholder="base64"></el-input> -->
        <el-upload
          class="avatar-uploader"
          :action="upUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="headers"
        >
          <img v-if="clockingPhotos" :src="clockingPhotos" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="打卡经度" prop="clockingLongitude">
        <el-input
          v-model="dataForm.clockingLongitude"
          placeholder="打卡经度"
        ></el-input>
      </el-form-item>
      <el-form-item label="打卡纬度" prop="clockingLatitude">
        <el-input
          v-model="dataForm.clockingLatitude"
          placeholder="打卡纬度"
        ></el-input>
      </el-form-item>
      <el-form-item label="人脸数据" prop="faceData">
        <el-input v-model="dataForm.faceData" placeholder="人脸数据"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      headers: { token: this.$cookie.get("token") },
      upUrl: this.$http.adornUrl(`/sys/getParamenters/changeBase64`),
      clockingPhotos: "",
      optionsprojectId: [],
      optionsDeviceSn: [],
      optionspersonnelName: [],
      optionsentryExit: [],
      visible: false,
      dataForm: {
        personnelId: "",
        id: 0,
        projectCode: "",
        deviceSn: "",
        personnelName: "",
        positionTitle: "",
        idNumber: "",
        attendanceTime: "",
        entryExit: "",
        clockingPhotos: "",
        clockingPhotosImgName: "",
        clockingLongitude: "",
        clockingLatitude: "",
        faceData: ""
      },
      dataRule: {
        projectCode: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        deviceSn: [
          { required: true, message: "设备序列号不能为空", trigger: "blur" }
        ],
        attendanceTime: [
          { required: true, message: "考勤时间不能为空", trigger: "blur" }
        ],
        entryExit: [
          { required: true, message: "进出口不能为空", trigger: "blur" }
        ],
        clockingPhotos: [
          { required: true, message: "打卡照不能为空", trigger: "blur" }
        ],
        clockingLongitude: [
          { required: true, message: "打卡经度不能为空", trigger: "blur" }
        ],
        clockingLatitude: [
          { required: true, message: "打卡纬度不能为空", trigger: "blur" }
        ],
        faceData: [
          { required: true, message: "人脸数据不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 选择项目
    changeCode(e) {
      //获取设备序列号
      this.$http({
        url: this.$http.adornUrl(
          `/sys/getParamenters/getBgateequipment?projectCode=${e}`
        ),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsDeviceSn = data.list;
        }
      });
      //获取人员
      this.$http({
        url: this.$http.adornUrl(
          `/sys/getParamenters/getPersonnelProfile?projectCode=${e}`
        ),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionspersonnelName = data.list;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(file);
      if (res.msg === "success") {
        this.dataForm.clockingPhotos = res.code;
        this.clockingPhotos = "data:image/png;base64," + res.code;
        this.dataForm.clockingPhotosImgName = file.name;
      }
    },
    beforeAvatarUpload(file) {},
    // 获取项目
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getProjectList`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsprojectId = data.list;
        }
      });

      //获取进出口
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getEntryExit`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsentryExit = data.list;
        }
      });
    },
    init(id) {
      this.getCompanyTypes();
      this.clockingPhotos = "";
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/battendancefalsedata/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectCode = data.bAttendanceFalseData.projectCode;
              this.changeCode(data.bAttendanceFalseData.projectCode);
              this.dataForm.deviceSn = data.bAttendanceFalseData.deviceSn;
              this.dataForm.personnelName = data.bAttendanceFalseData.personnelName;
              this.dataForm.positionTitle = data.bAttendanceFalseData.positionTitle;
              this.dataForm.idNumber = data.bAttendanceFalseData.idNumber;
              this.dataForm.attendanceTime =
                data.bAttendanceFalseData.attendanceTime;
              this.dataForm.entryExit = data.bAttendanceFalseData.entryExit;
              this.dataForm.clockingPhotos =
                data.bAttendanceFalseData.clockingPhotos;
              this.clockingPhotos = data.bAttendanceFalseData.clockingPhotos;
              this.dataForm.personnelId = data.bAttendanceFalseData.personnelId;
              this.dataForm.clockingLongitude =
                data.bAttendanceFalseData.clockingLongitude;
              this.dataForm.clockingLatitude =
                data.bAttendanceFalseData.clockingLatitude;
              this.dataForm.faceData = data.bAttendanceFalseData.faceData;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/battendancefalsedata/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              projectCode: this.dataForm.projectCode,
              deviceSn: this.dataForm.deviceSn,
              personnelName: this.dataForm.personnelName,
              clockingPhotosImgName: this.dataForm.clockingPhotosImgName,
              personnelId: this.dataForm.personnelId,
              positionTitle: this.dataForm.positionTitle,
              idNumber: this.dataForm.idNumber,
              attendanceTime: this.dataForm.attendanceTime,
              entryExit: this.dataForm.entryExit,
              clockingPhotos: this.dataForm.clockingPhotos,
              clockingLongitude: this.dataForm.clockingLongitude,
              clockingLatitude: this.dataForm.clockingLatitude,
              faceData: this.dataForm.faceData
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
<style>
.up_class .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.up_class .avatar-uploader .el-upload:hover {
  border-color: #17b3a3;
}
.up_class .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.up_class .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
